/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package GUI.ThuePhong;

import DTO.CTPhieuDatPhong_DTO;
import DTO.KhachHang_DTO;
import DTO.PhieuDatPhong_DTO;
import Define.*;
import GUI.Utilities;
import Server.QLKS_Interface;
import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Mr
 */
public final class JIFDatPhong extends javax.swing.JInternalFrame {

    DefaultTableModel modelPhongTrong;
    DefaultTableModel modelPhongDat;
    DefaultTableModel modelThongTinKhach;
    String ServerURL;
    QLKS_Interface QI;

    /**
     * Creates new form JIFDatPhong
     */
    public JIFDatPhong() {
        initComponents();
        this.setSize(880, 560);
        ServerURL = "qlks";
        try {
            QI = (QLKS_Interface) Naming.lookup(ServerURL);
        } catch (NotBoundException | MalformedURLException | RemoteException ex) {
            Logger.getLogger(JIFDatPhong.class.getName()).log(Level.SEVERE, null, ex);
        }
        showDSPhongTrong();
        showDSPhongThue();
    }

    public void showDSPhongTrong() {
        try {
            modelPhongTrong = QI.LoadTableModel(Define.YC_LAYDS_PHONGTRONG);
            tablePhongTrong.setModel(modelPhongTrong);
        } catch (RemoteException e) {
        }
    }

    public void showDSPhongThue() {
        Vector column = new Vector();
        column.add("Mã Phòng");
        column.add("Tên Loại Phòng");
        column.add("Đơn Giá");
        modelPhongDat = new DefaultTableModel(column, 0);
        tablebPhongDat.setModel(modelPhongDat);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        tfTenKhachHang = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        tfCMND = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        tfTriGia = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        tfemail = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        dpNgayDen = new org.jdesktop.swingx.JXDatePicker();
        jLabel6 = new javax.swing.JLabel();
        dpNgayDi = new org.jdesktop.swingx.JXDatePicker();
        btLapPhieu = new javax.swing.JButton();
        jLabel7 = new javax.swing.JLabel();
        cbbSoKhach = new javax.swing.JComboBox();
        btThoat = new javax.swing.JButton();
        jLabel8 = new javax.swing.JLabel();
        tfSoDienThoai = new javax.swing.JTextField();
        btLapMoi = new javax.swing.JButton();
        Pannel = new javax.swing.JScrollPane();
        tablePhongTrong = new javax.swing.JTable();
        Pannel2 = new javax.swing.JScrollPane();
        tablebPhongDat = new javax.swing.JTable();

        setClosable(true);
        setIconifiable(true);
        setMaximizable(true);
        setTitle("ĐẶT PHÒNG");

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Thông tin khách hàng"));
        jPanel1.setName("Thông tin khách hàng"); // NOI18N

        jLabel1.setText("Tên Khách Hàng");

        jLabel2.setText("CMND");

        jLabel3.setText("Số điện thoại");

        tfTriGia.setEditable(false);
        tfTriGia.setBackground(new java.awt.Color(204, 204, 204));

        jLabel4.setText("Email");

        jLabel5.setText("Ngày đến");

        dpNgayDen.setDate(new Date(System.currentTimeMillis()));
        dpNgayDen.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                dpNgayDenActionPerformed(evt);
            }
        });

        jLabel6.setText("Ngày đi");

        dpNgayDi.setDate(new Date(System.currentTimeMillis()));
        dpNgayDi.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                dpNgayDiActionPerformed(evt);
            }
        });

        btLapPhieu.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Image/1371825243_Apply.png"))); // NOI18N
        btLapPhieu.setText("Lập Phiếu");
        btLapPhieu.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btLapPhieuActionPerformed(evt);
            }
        });

        jLabel7.setText("Số người:");

        cbbSoKhach.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30" }));

        btThoat.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Image/close.png"))); // NOI18N
        btThoat.setText("Thoát");
        btThoat.setPreferredSize(new java.awt.Dimension(79, 23));
        btThoat.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btThoatActionPerformed(evt);
            }
        });

        jLabel8.setText("Trị giá");

        tfSoDienThoai.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                tfSoDienThoaiActionPerformed(evt);
            }
        });

        btLapMoi.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Image/Sync.png"))); // NOI18N
        btLapMoi.setText("Lập mới");
        btLapMoi.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btLapMoiActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(95, 95, 95)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel8)
                    .addComponent(jLabel7)
                    .addComponent(jLabel5)
                    .addComponent(jLabel3)
                    .addComponent(jLabel1))
                .addGap(40, 40, 40)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(cbbSoKhach, javax.swing.GroupLayout.PREFERRED_SIZE, 52, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(dpNgayDen, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(tfTenKhachHang)
                    .addComponent(tfTriGia)
                    .addComponent(tfSoDienThoai, javax.swing.GroupLayout.PREFERRED_SIZE, 166, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(jLabel2)
                        .addComponent(jLabel4))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel6)
                        .addGap(5, 5, 5)))
                .addGap(34, 34, 34)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(tfemail)
                    .addComponent(tfCMND)
                    .addComponent(dpNgayDi, javax.swing.GroupLayout.PREFERRED_SIZE, 166, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(btLapMoi)
                .addGap(18, 18, 18)
                .addComponent(btLapPhieu)
                .addGap(18, 18, 18)
                .addComponent(btThoat, javax.swing.GroupLayout.PREFERRED_SIZE, 94, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(150, 150, 150))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(tfTenKhachHang, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1)
                    .addComponent(jLabel2)
                    .addComponent(tfCMND, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(jLabel4)
                    .addComponent(tfemail, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(tfSoDienThoai, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(dpNgayDen, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel6)
                    .addComponent(dpNgayDi, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel7)
                    .addComponent(cbbSoKhach, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel8)
                    .addComponent(tfTriGia, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btThoat, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btLapMoi)
                    .addComponent(btLapPhieu)))
        );

        Pannel.setBorder(javax.swing.BorderFactory.createTitledBorder("Thông tin phòng trống"));

        tablePhongTrong.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {},
                {},
                {},
                {}
            },
            new String [] {

            }
        ));
        tablePhongTrong.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tablePhongTrongMouseClicked(evt);
            }
        });
        Pannel.setViewportView(tablePhongTrong);

        Pannel2.setBorder(javax.swing.BorderFactory.createTitledBorder("Thông tin phòng chọn"));

        tablebPhongDat.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {

            }
        ));
        tablebPhongDat.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tablebPhongDatMouseClicked(evt);
            }
        });
        Pannel2.setViewportView(tablebPhongDat);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addComponent(Pannel, javax.swing.GroupLayout.PREFERRED_SIZE, 414, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(Pannel2, javax.swing.GroupLayout.PREFERRED_SIZE, 326, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(Pannel, javax.swing.GroupLayout.DEFAULT_SIZE, 225, Short.MAX_VALUE)
                    .addComponent(Pannel2, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btLapPhieuActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btLapPhieuActionPerformed
        // TODO add your handling code here:

        //Them khach hang
        String makhach;
        String makhachmax;
        try {
            makhachmax = QI.LayMaMax(Define.YC_LAYMAMAX_KHACHHANG);
            if (makhachmax.equals("")) {
                makhach = Utilities.taoMaTuDong("MK000000", 2);

            } else {
                makhach = Utilities.taoMaTuDong(makhachmax, 2);
            }
            String tenKhach = tfTenKhachHang.getText();
            String sdt = tfSoDienThoai.getText();
            String email = tfemail.getText();
            String CMND = tfCMND.getText();
            String tinhtrang = "Đang đặt";
            if (tenKhach.equals("")) {
                JOptionPane.showMessageDialog(rootPane, "Bạn phải nhập tên của khách!");
                return;
            }
            if (CMND.equals("")) {
                JOptionPane.showMessageDialog(rootPane, "Bạn phải nhập CMND (Hộ chiếu) của khách!");
                return;
            }

            //them phieudat
            int row = tablebPhongDat.getRowCount();
            if (row <= 0) {
                JOptionPane.showMessageDialog(rootPane, "Bạn phải chọn phòng cho khách!");
                return;
            }
            String maphieu;
            String maphieumax;
            maphieumax = QI.LayMaMax(Define.YC_LAYMAMAX_PHIEUDATPHONG);
            if (maphieumax.equals("")) {
                maphieu = Utilities.taoMaTuDong("MPD000000", 3);

            } else {
                maphieu = Utilities.taoMaTuDong(maphieumax, 3);

            }
            String sokhach = cbbSoKhach.getSelectedItem().toString();
            SimpleDateFormat format = new SimpleDateFormat("MM-dd-yyyy");

            Date dngayden = dpNgayDen.getDate();
            Date dngaydi = dpNgayDi.getDate();
            String ngayden = format.format(dngayden);
            String ngaydi = format.format(dngaydi);
            Date today = new Date(System.currentTimeMillis() - 100742784);
            int sosanh = dngayden.compareTo(today);
            if (sosanh < 0) {
                JOptionPane.showMessageDialog(rootPane, "Ngày đến phải bằng hoặc sau hiện tại!");
                return;
            }
            if (dngaydi.before(dngayden)) {
                JOptionPane.showMessageDialog(rootPane, "Ngày đi phải lớn hơn hoặc bằng ngày đến!");
                return;
            }
            float dongia = 0;
            for (int i = 0; i < row; i++) {
                dongia += Float.parseFloat(tablebPhongDat.getValueAt(i, 2).toString());
            }
            long songaythue = (dngaydi.getTime() - dngayden.getTime()) / 86400000 + 1;
            long trigia = (long) dongia * songaythue;
            tfTriGia.setText(Long.toString(trigia));

            //showThanhTien();
            KhachHang_DTO kh = new KhachHang_DTO();
            kh.setMaKhachHang(makhach);
            kh.setTenKhachHang(tenKhach);
            kh.setCmnd(CMND);
            kh.setDienThoai(sdt);
            kh.setEmail(email);
            kh.setTinhTrang(tinhtrang);
            QI.InsertKhachHang(kh);

            PhieuDatPhong_DTO p = new PhieuDatPhong_DTO();
            p.setMaPhieuDat(maphieu);
            p.setMaKhach(makhach);
            p.setNgayden(ngayden);
            p.setNgaydi(ngaydi);
            p.setSoKhach(sokhach);
            p.setTrigia(tfTriGia.getText());

            QI.InsertPhieuDatPhong(p);

            //them ct phieu dat
            String mactphieu;
            String mactphieumax;

            for (int i = 0; i < row; i++) {
                mactphieumax = QI.LayMaMax(Define.YC_LAYMAMAX_CTPHIEUDATPHONG);
                if (mactphieumax.equals("")) {
                    mactphieu = Utilities.taoMaTuDong("MCTPD000000", 5);

                } else {
                    mactphieu = Utilities.taoMaTuDong(mactphieumax, 5);
                }
                CTPhieuDatPhong_DTO ct = new CTPhieuDatPhong_DTO();
                ct.setMaCTPhieuDat(mactphieu);
                ct.setMaPhieuDat(maphieu);
                ct.setMaPhong(tablebPhongDat.getValueAt(i, 0).toString());
                QI.CapNhatTinhTrangPhong(tablebPhongDat.getValueAt(i, 0).toString(), "Đang đặt");
                QI.InsertCTPhieuDatPhong(ct);
            }
            JOptionPane.showMessageDialog(rootPane, "Lập phiếu thành công!");
            btLapMoiActionPerformed(evt);
        } catch (Exception e) {
        }
    }//GEN-LAST:event_btLapPhieuActionPerformed

    private void btThoatActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btThoatActionPerformed
        // TODO add your handling code here:
        this.dispose();
    }//GEN-LAST:event_btThoatActionPerformed

    private void tfSoDienThoaiActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_tfSoDienThoaiActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_tfSoDienThoaiActionPerformed

    private void btLapMoiActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btLapMoiActionPerformed
        // TODO add your handling code here:
        tfTenKhachHang.setText("");
        tfCMND.setText("");
        tfSoDienThoai.setText("");
        tfemail.setText("");
        tfTriGia.setText("");
        modelPhongDat.setNumRows(0);
    }//GEN-LAST:event_btLapMoiActionPerformed

    private void tablePhongTrongMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tablePhongTrongMouseClicked
        // TODO add your handling code here:
        int row = tablePhongTrong.getSelectedRow();
        Vector rowData = new Vector();
        String maPhong = (String) this.modelPhongTrong.getValueAt(row, 0).toString();
        String tenLoaiPhong = (String) this.modelPhongTrong.getValueAt(row, 1).toString();
        String donGia = (String) this.modelPhongTrong.getValueAt(row, 2).toString();
        rowData.add(maPhong);
        rowData.add(tenLoaiPhong);
        rowData.add(donGia);
        modelPhongDat.addRow(rowData);
        tablebPhongDat.setModel(modelPhongDat);
        modelPhongTrong.removeRow(row);
        showThanhTien();
    }//GEN-LAST:event_tablePhongTrongMouseClicked

    private void tablebPhongDatMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tablebPhongDatMouseClicked
        // TODO add your handling code here:
        // TODO add your handling code here:
        int row = tablebPhongDat.getSelectedRow();
        if (row < 0 || row > tablebPhongDat.getRowCount()) {
            return;
        }
        Vector rowData = new Vector();
        String maPhong = (String) this.modelPhongDat.getValueAt(row, 0).toString();
        String tenLoaiPhong = (String) this.modelPhongDat.getValueAt(row, 1).toString();
        String donGia = (String) this.modelPhongDat.getValueAt(row, 2).toString();
        rowData.add(maPhong);
        rowData.add(tenLoaiPhong);
        rowData.add(donGia);
        modelPhongTrong.addRow(rowData);
        tablePhongTrong.setModel(modelPhongTrong);
        modelPhongDat.removeRow(row);
    }//GEN-LAST:event_tablebPhongDatMouseClicked

    private void dpNgayDenActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_dpNgayDenActionPerformed
        // TODO add your handling code here:
        Date dngayden = dpNgayDen.getDate();
        Date dngaydi = dpNgayDi.getDate();
        Date today = new Date(System.currentTimeMillis() - 100742784);
        int sosanh = dngayden.compareTo(today);
        if (sosanh < 0) {
            JOptionPane.showMessageDialog(rootPane, "Ngày đến phải bằng hoặc sau hiện tại!");
            dpNgayDen.setDate(new Date(System.currentTimeMillis()));
            return;
        }
        if (dngaydi.before(dngayden)) {
            dpNgayDi.setDate(dpNgayDen.getDate());
            return;
        }
        showThanhTien();
    }//GEN-LAST:event_dpNgayDenActionPerformed

    private void dpNgayDiActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_dpNgayDiActionPerformed
        // TODO add your handling code here:
        Date dngayden = dpNgayDen.getDate();
        Date dngaydi = dpNgayDi.getDate();
        if (dngaydi.before(dngayden)) {
            JOptionPane.showMessageDialog(rootPane, "Ngày đi phải lớn hơn hoặc bằng ngày đến!");
            dpNgayDi.setDate(dpNgayDen.getDate());
            return;
        }
        showThanhTien();
    }//GEN-LAST:event_dpNgayDiActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JScrollPane Pannel;
    private javax.swing.JScrollPane Pannel2;
    private javax.swing.JButton btLapMoi;
    private javax.swing.JButton btLapPhieu;
    private javax.swing.JButton btThoat;
    private javax.swing.JComboBox cbbSoKhach;
    private org.jdesktop.swingx.JXDatePicker dpNgayDen;
    private org.jdesktop.swingx.JXDatePicker dpNgayDi;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JTable tablePhongTrong;
    private javax.swing.JTable tablebPhongDat;
    private javax.swing.JTextField tfCMND;
    private javax.swing.JTextField tfSoDienThoai;
    private javax.swing.JTextField tfTenKhachHang;
    private javax.swing.JTextField tfTriGia;
    private javax.swing.JTextField tfemail;
    // End of variables declaration//GEN-END:variables

    private void showThanhTien() {
        Date dngayden = dpNgayDen.getDate();
        Date dngaydi = dpNgayDi.getDate();
        int row = tablebPhongDat.getRowCount();
        float dongia = 0;
        for (int i = 0; i < row; i++) {
            dongia += Float.parseFloat(tablebPhongDat.getValueAt(i, 2).toString());
        }
        long songaythue = (dngaydi.getTime() - dngayden.getTime()) / 86400000 + 1;
        long trigia = (long) dongia * songaythue;
        tfTriGia.setText(Long.toString(trigia));
    }
}
